;;;    lambda-tube (λ-tube) - A youtube search API and application in scheme.
;;;    Copyright (C) 2008  Abhishek Mishra
;;;
;;;    This program is free software: you can redistribute it and/or modify
;;;    it under the terms of the GNU General Public License as published by
;;;    the Free Software Foundation, either version 3 of the License, or
;;;    (at your option) any later version.

;;;    This program is distributed in the hope that it will be useful,
;;;    but WITHOUT ANY WARRANTY; without even the implied warranty of
;;;    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;;    GNU General Public License for more details.

;;;    You should have received a copy of the GNU General Public License
;;;    along with this program.  If not, see <http://www.gnu.org/licenses/>.

;Loads the search history.

(module lt-search-history mzscheme
  
  (require "lt-preferences.scm")
  
  (provide
   load-search-history
   get-search-history
   add-search-item-to-history
   save-search-history
   set-history-size
   get-history-size
   clear-history)
  
  ;initialize history size to a default value of 10
  (define *history-size* 10)
  
  ;initialize history to empty
  (define *history* '())
  
  ;load the history list
  (define (load-search-history)
    (set! *history* '())
    (if (file-exists? (get-history-file-name))
        (with-input-from-file (path->string (get-history-file-name))
          (lambda () (set! *history* (read))))))
  
  ;save the search hitory
  (define (save-search-history)
    (let ((history-fname (get-history-file-name)))
      (if (file-exists? history-fname)
          (delete-file history-fname))
      (with-output-to-file (path->string history-fname) (lambda () (write *history*)))))
  
  ;adds a search item to the history
  (define (add-search-item-to-history search-text)
    (if (not (member search-text *history*))
        (begin
          (set! *history* (append *history* (list search-text)))
          (if (> (length *history*) *history-size*)
              (set! *history* (list-tail *history* 1)))
          #t)
        #f))
  
  (define (get-search-history)
    *history*)
  
  ;set the history size
  (define (set-history-size size)
    (set! *history-size* size)
    (if (> (length *history*) *history-size*)
        (set! *history* (list-tail *history* (- (length *history*) *history-size*)))))
  
  ;get the history size
  (define (get-history-size)
    *history-size*)
  
  ;clear the history
  (define (clear-history)
    (set! *history* '()))
  )